package com.demoo.zjsu.service.smartwebsite.zhaoban;

import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.service.smartwebsite.Page;
import com.demoo.zjsu.service.smartwebsite.PageFactoryExtend;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jshjsh on 2016/9/9.
 */
@Service
public class IndexService extends PageFactoryExtend {

    @Override
    public List<String> getTimes(Elements elements) {
        ArrayList<String> arrayList = new ArrayList<String>();
        for (Element element : elements) {
            String time = element.select(".HSATimeStamp").text();
            arrayList.add(time);
        }

        return arrayList;
    }

    @Override
    public Elements getOtherMain(Document document) {
        return document.select("[width=938]").eq(0);
    }

    @Override
    public PageTwo dealTitle(String url, String title) {
        url = url.replace("html", "").substring(3);
        return new PageTwo(title, "", url, 0);
    }

    @Override
    public void filterAgain(Elements elements) {
        elements.select("p").eq(0).remove();
        Pattern pattern = Pattern.compile("http://zhaoban.zjsu.edu.cn/html/.*htm");
        Elements a = elements.select("a");
        for (Element element : a) {
            String src = element.attr("href");
            Matcher matcher = pattern.matcher(src);
            if (matcher.matches()) {
                src = src.replace("http://zhaoban.zjsu.edu.cn/html/", "../pagetwocontent/detail4.zjgsu?key=");
                element.attr("href", src);
            }
        }
    }

    @Override
    public PageTwoContent getContent(Elements elements) {
        String content = elements.toString();
        return new PageTwoContent(content,"");
    }

    @Override
    public Page getPage() {
        return new Page("http://zhaoban.zjsu.edu.cn/indexzb.asp", "最新消息");
    }

    @Override
    public Elements getMain(Document document) {
        return document.select("[width=104%]").eq(0).select("tr");
    }
}
